package cn.yunhe.springmvc.controller;

import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import cn.yunhe.springmvc.exception.AuthException;
import cn.yunhe.springmvc.exception.Result;

@RestControllerAdvice
public class GlobalExceptionHandler {

	private static Logger logger = Logger.getLogger(GlobalExceptionHandler.class);

	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(AuthException.class)
	public Result handleServiceException(Exception exception, HttpServletRequest request) {
		return new Result(400,exception.getMessage(),null);
	}

	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(Exception.class)
	public Result handleException(Exception exception, HttpServletRequest request) {
		logger.error("系统异常!", exception);
		return new Result(400,exception.getMessage(),null);
	}

}
